package com.mall.controller;

import com.mall.domain.ItemGoods;
import com.mall.domain.Page;
import com.mall.service.impl.ItemGoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品web层
 * @author : liangY
 * @date : 2021-01-12 18:37
 */
@Controller
@RequestMapping("/items")
public class ItemGoodsController {

    @Autowired
    private ItemGoodsServiceImpl itemGoodsService;

    /**
     * 查询所有商品
     * @param page
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(@RequestBody Page page){
        Map<String,Object> itemMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //获取会员集合
        List<ItemGoods> items =itemGoodsService.findAll(page);
        //放入dataMap集合
        dataMap.put("items",items);
        dataMap.put("total",itemGoodsService.findTotal());
        //放入Map集合
        itemMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取商品列表成功");
        metaMap.put("status",200);

        itemMap.put("meta",metaMap);
        return itemMap;
    }
    @RequestMapping("/findAll1")
    public  @ResponseBody
    Map<String,Object> getfindAll1() {
        Map<String,Object> itemdataMap = new HashMap<>();
        //获取轮播图集合
        List<ItemGoods> itemGoods = itemGoodsService.findAll1();
        //放入Map集合
        itemdataMap.put("message",itemGoods);

        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取轮播图成功");
        metaMap.put("status",200);

        itemdataMap.put("meta",metaMap);
        return itemdataMap;
    }

    /***
     * 用来展示前台首页
     * @return
     */
    @RequestMapping("/findAllimg")
    public  @ResponseBody
    Map<String,Object> getfindAllimg() {
        Map<String,Object> itemdataMap = new HashMap<>();
        //获取轮播图集合
        List<ItemGoods> itemGoods = itemGoodsService.findAllimg();
        //放入Map集合
        itemdataMap.put("message",itemGoods);

        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","首页商品获取成功");
        metaMap.put("status",200);

        itemdataMap.put("meta",metaMap);
        return itemdataMap;
    }
    @RequestMapping("/findAb")
    public  @ResponseBody
    Map<String,Object> getfindAb(Integer goods_id) {
        Map<String,Object> itemdataMap = new HashMap<>();
        //获取轮播图集合
        List<ItemGoods> itemGoods = itemGoodsService.findAb(goods_id);
        //放入Map集合
        itemdataMap.put("message",itemGoods);

        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","ok");
        metaMap.put("status",200);

        itemdataMap.put("meta",metaMap);
        return itemdataMap;
    }



    /**
     * 查询所有商品
     * @param page
     * @return
     */
    @RequestMapping("/findAlls")
    public @ResponseBody
    Map<String, Object> findAlls(@RequestBody Page page){
        Map<String,Object> itemMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //获取会员集合
        List<ItemGoods> items =itemGoodsService.findAlls(page);
        //放入dataMap集合
        dataMap.put("items",items);
        dataMap.put("total",itemGoodsService.findTotal());
        //放入Map集合
        itemMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取商品列表成功");
        metaMap.put("status",200);

        itemMap.put("meta",metaMap);
        return itemMap;
    }
    /**
     * 更新指定商品
     * @param itemGoods
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody
    Map<String, Object> update(@RequestBody ItemGoods itemGoods){
        Map<String,Object> itemMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("items",itemGoods);
        //放入Map集合
        itemMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = itemGoodsService.update(itemGoods);
        if(flag > 0){
            metaMap.put("msg","更新商品成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新商品失败");
            metaMap.put("status",0);
        }
        itemMap.put("meta",metaMap);
        return itemMap;
    }



    /**
     * 添加商品
     * @param itemGoods
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody
    Map<String, Object> add(@RequestBody ItemGoods itemGoods){
        Map<String,Object> itemMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("items",itemGoods);
        //放入Map集合
        itemMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = itemGoodsService.add(itemGoods);
        if(flag > 0){
            metaMap.put("msg","添加商品成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新商品失败");
            metaMap.put("status",0);
        }
        itemMap.put("meta",metaMap);
        return itemMap;
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public @ResponseBody
    Map<String, Object> add(Integer id){
        Map<String,Object> itemMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        itemMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = itemGoodsService.del(id);
        if(flag > 0){
            metaMap.put("msg","删除商品成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除商品失败");
            metaMap.put("status",0);
        }
        itemMap.put("meta",metaMap);
        return itemMap;
    }
}
